package com.sheledon.flyCat.core.factory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sheledon.flyCat.network.socket.http.HttpResponse;
import com.sheledon.flyCat.network.socket.http.enum_.HttpStatus;

import java.util.HashMap;

public class HttpResponseFactory {
    private static final String CONTENT_TYPE = "application/json;charset=utf-8";
    public static HttpResponse getErrorResponse(){
        HttpResponse response = new HttpResponse();
        HashMap<String,String> headers = new HashMap<>();
        headers.put("content-type",CONTENT_TYPE);
        response.setStatus(HttpStatus.SERVER_ERROR);
        response.setResponseHeaders(headers);
        return response;
    }
    public  static HttpResponse getSuccessResponse(){
        HttpResponse response = new HttpResponse();
        HashMap<String,String> headers = new HashMap<>();
        headers.put("content-type",CONTENT_TYPE);
        response.setStatus(HttpStatus.OK);
        response.setResponseHeaders(headers);
        return response;
    }
    public static HttpResponse buildSuccessResponse(Object o) throws JsonProcessingException {
        HttpResponse response = new HttpResponse();
        HashMap<String,String> headers = new HashMap<>();
        headers.put("content-type",CONTENT_TYPE);
        response.setStatus(HttpStatus.OK);
        response.setResponseHeaders(headers);
        ObjectMapper om = new ObjectMapper();
        response.setBody(om.writeValueAsString(o));
        return response;
    }
    public static HttpResponse getNotFoundResponse(){
        HttpResponse response = new HttpResponse();
        response.setStatus(HttpStatus.NOT_FOUND);
        return response;
    }
}
